package com.fenghuajueli.module_user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_ad.AdShowUtils;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.mvp.OtherBaseFragment;
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity;
import com.fenghuajueli.libbasecoreui.ui.FeedBackActivity;
import com.fenghuajueli.libbasecoreui.ui.PrivacyWebActivity;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.utils.JumpActivityUtils;
import com.fenghuajueli.libbasecoreui.utils.PrivacyConstantsUtils;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.databinding.FragmentUserMineBinding;

/**
 * 我的页面，弄成fragment方便使用
 */
@Route(path = UserModuleRoute.FRAGMENT_USER_MINE_SEANCE)
public class UserMineFragment extends OtherBaseFragment implements View.OnClickListener {

    FragmentUserMineBinding binding;


    @Override
    public void initView(Bundle savedInstanceState) {
        binding = FragmentUserMineBinding.bind(getContentView());
        binding.tvYjfk.setOnClickListener(this);
        binding.tvQlhc.setOnClickListener(this);
        binding.tvYszc.setOnClickListener(this);
        binding.tvGywm.setOnClickListener(this);
        binding.tvkefu.setOnClickListener(this);
        binding.userInfoContainer.setOnClickListener(this);
        if (!UserInfoUtils.getInstance().isVip() && SwitchKeyUtils.getAdStatus()) {
            AdShowUtils.getInstance().loadBannerAd(getActivity(), "banner_ad_mine", binding.containerAd);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initUserInfo();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_yjfk) {
            startActivity(new Intent(getActivity(), FeedBackActivity.class));
        } else if (view.getId() == R.id.tv_qlhc) {
            ToastUtils.showShort("清理成功");
        } else if (view.getId() == R.id.tvkefu) {
            CustomerServiceActivity.start(requireContext());
        } else if (view.getId() == R.id.tv_yszc) {
            PrivacyWebActivity.start(getContext(), PrivacyConstantsUtils.ProtocolType.PRIVACY);
        } else if (view.getId() == R.id.tv_gywm) {
            JumpActivityUtils.goAboutUsActivity(getContext());
        } else if (view.getId() == R.id.userInfoContainer) {
            if (UserInfoUtils.getInstance().isLogin()) {
                ARouter.getInstance().build(UserModuleRoute.USER_INFO_ACTIVITY).navigation();
            } else {
                ARouter.getInstance().build(UserModuleRoute.USER_LOGIN).navigation();
            }
        }
    }

    private void initUserInfo() {
        if (UserInfoUtils.getInstance().isLogin()) {
            binding.tvUserName.setText(UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getUser_name());
            binding.tvUserId.setText("UID：" + UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getUser_id());
        } else {
            binding.tvUserName.setText("未登录");
            binding.tvUserId.setText("快去登录吧！");
            binding.ivUserVipStatus.setImageResource(R.mipmap.icon_members_01);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        AdShowUtils.getInstance().destroyBanner("banner_ad_mine");
        binding = null;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_user_mine;
    }
}
